export const isValidUsername = (str: string) =>
  ['admin', 'editor'].indexOf(str.trim()) >= 0

export const isExternal = (path: string) => /^(https?:|mailto:|tel:)/.test(path)

export const isArray = (arg: any) => {
  if (typeof Array.isArray === 'undefined') {
    return Object.prototype.toString.call(arg) === '[object Array]'
  }
  return Array.isArray(arg)
}

export const isValidURL = (url: string) => {
  const reg = /^(https?|ftp):\/\/([a-zA-Z0-9.-]+(:[a-zA-Z0-9.&%$-]+)*@)*((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9][0-9]?)(\.(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9]?[0-9])){3}|([a-zA-Z0-9-]+\.)*[a-zA-Z0-9-]+\.(com|edu|gov|int|mil|net|org|biz|arpa|info|name|pro|aero|coop|museum|[a-zA-Z]{2}))(:[0-9]+)*(\/($|[a-zA-Z0-9.,?'\\+&%$#=~_-]+))*$/
  return reg.test(url)
}

/**
 * 判断是否为空
 * @param val
 */
export function isEmpty(val: any) {
  if (val instanceof Array) {
    if (val.length === 0) {
      return true
    }
  } else if (val instanceof Object) {
    if (JSON.stringify(val) === '{}') {
      return true
    }
  } else {
    if (
      val === 'null' ||
      val === null ||
      val === 'undefined' ||
      val === undefined ||
      val === ''
    ) {
      return true
    }
    return false
  }
  return false
}
/**
 * 判断是否为空
 * @param val
 */
export function isNotEmpty(val: any) {
  return !isEmpty(val)
}

export function isInt(str: string) {
  const re = /^((-\d+)|(0+)|\d+)$/
  return re.test(str)
}

export function isImageUrl(str: string) {
  const re = /^(https?|http).*(gif|png|jpe?g)$/
  return re.test(str)
}

// 是否是邮件
export function isEmail(email: string) {
  const re = /^[a-zA-Z0-9-.+_]+@[a-zA-Z0-9-]+(\.([a-zA-Z0-9-]{2,10}))+$/ // eslint-disable-line
  return re.test(email)
}
